using System;
using System.Collections.Generic;
using BillsDomain;
using BillsDataAccess;

[assembly: CLSCompliant(true)]
namespace BillsPresentation
{
    /// <summary>
    /// Scheduled payment controller that persists to disk
    /// </summary>
    public class ScheduledPaymentController : IScheduledPaymentController
    {
        private int selectedPaymentId = -1;

        /// <summary>
        /// Occurs when the scheduled payment list changes.
        /// </summary>
        public event EventHandler ListChanged;

        /// <summary>
        /// Gets the scheduled payments.
        /// </summary>
        /// <value>The scheduled payments.</value>
        public List<ScheduledPayment> ScheduledPayments
        {
            get { return scheduledPayments; }
        }

        /// <summary>
        /// Gets the next payment id.
        /// </summary>
        /// <value>The next payment id.</value>
        private int NextPaymentId
        {
            get
            {
                int result = -1;
                foreach (ScheduledPayment scheduledPayment in this.ScheduledPayments)
                {
                    if (scheduledPayment.Id > result)
                        result = scheduledPayment.Id;
                }

                result++;
                return result;
            }
        }

        /// <summary>
        /// Gets the current scheduled payment.
        /// </summary>
        /// <value>The current scheduled payment.</value>
        public ScheduledPayment CurrentScheduledPayment
        {
            get
            {
                ScheduledPayment result = null;
                foreach (ScheduledPayment scheduledPayment in this.ScheduledPayments)
                {
                    if (scheduledPayment.Id == this.selectedPaymentId)
                        result = scheduledPayment;
                }

                return result;
            }
        }

        /// <summary>
        /// Updates the list with the given payment.
        /// </summary>
        /// <param name="payment">The payment.</param>
        public void UpdateListWith(ScheduledPayment payment)
        {
            if (payment == null)
            {
                throw new ArgumentNullException("payment");
            }

            ScheduledPayment paymentToChange;
            if (payment.Id >= 0)
            {
                paymentToChange = this.CurrentScheduledPayment;
            }
            else
            {
                paymentToChange = new ScheduledPayment();
                paymentToChange.Id = this.NextPaymentId;
                this.ScheduledPayments.Add(paymentToChange);
            }

            paymentToChange.Amount = payment.Amount;
            paymentToChange.FirstDate = payment.FirstDate;
            paymentToChange.Payee = payment.Payee;
            paymentToChange.PaymentRecurrence = payment.PaymentRecurrence;
            paymentToChange.PaymentType = payment.PaymentType;
            paymentToChange.IncludeInProjection = payment.IncludeInProjection;
            this.billsData.SavePayments(this.ScheduledPayments);
            if (this.ListChanged != null)
            {
                this.ListChanged(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// Sets the scheduled payment.
        /// </summary>
        /// <param name="id">The id.</param>
        public void SetScheduledPayment(int id)
        {
            this.selectedPaymentId = id;
        }

        private List<ScheduledPayment> scheduledPayments;

        private IBillsData billsData;

        /// <summary>
        /// Saves the data.
        /// </summary>
        public void SaveData()
        {
            this.billsData.SavePayments(this.ScheduledPayments);
        }

        /// <summary>
        /// Loads the data.
        /// </summary>
        public void LoadData()
        {
            this.scheduledPayments = this.billsData.RetrievePayments();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ScheduledPaymentController"/> class.
        /// </summary>
        public ScheduledPaymentController()
            : this(new BillsData())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ScheduledPaymentController"/> class.
        /// </summary>
        /// <param name="billsData">The bills data.</param>
        public ScheduledPaymentController(IBillsData billsData)
        {
            this.billsData = billsData;
            this.scheduledPayments = new List<ScheduledPayment>();
        }

        /// <summary>
        /// Deletes the current payment.
        /// </summary>
        public void DeleteCurrentPayment()
        {
            this.scheduledPayments.Remove(this.CurrentScheduledPayment);
            this.billsData.SavePayments(this.ScheduledPayments);
            if (this.ListChanged != null)
            {
                this.ListChanged(this, EventArgs.Empty);
            }
        }

        public void SaveData(string pathAndFilename)
        {
            this.billsData.SavePayments(this.ScheduledPayments, pathAndFilename);
        }

        public void LoadData(string pathAndFileName)
        {
            this.scheduledPayments = this.billsData.RetrievePayments(pathAndFileName);
        }
    }
}
